//
//  RXFrameViewController.m
//  RXFrame
//
//  Created by srxboys on 2021/4/20.
//

#import "RXFrameViewController.h"
#import <RXframe/RXFrameDefine.h>

@class ViewKeyboardDeal;

@interface RXFrameViewController ()<UIGestureRecognizerDelegate>
{
    UIControl       * _noneNetworkControl;
    UIButton        * _noneNetworkImgButton;
    UIButton        * _noneNetworkLabelButton;
}
@property (nonatomic, strong) ViewKeyboardDeal * viewKeyboardDeal;
@property (nonatomic, strong) UITapGestureRecognizer * viewKeyboardDealTap;
@end

@implementation RXFrameViewController
@synthesize networkStatus = _networkStatus;

//改变 控制器的 状态栏的 颜色
- (UIStatusBarStyle)preferredStatusBarStyle {
    return UIStatusBarStyleLightContent;
}

- (BOOL)shouldAutorotate
{
    return YES;
}


- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
    if(!self.isRotate) {
        return UIInterfaceOrientationPortrait;
    }
    return UIInterfaceOrientationPortrait|UIInterfaceOrientationLandscapeLeft|UIInterfaceOrientationLandscapeRight;
}

- (UIInterfaceOrientationMask)supportedInterfaceOrientations
{
    if(!self.isRotate) {
        return UIInterfaceOrientationMaskPortrait;
    }
    return UIInterfaceOrientationMaskPortrait|UIInterfaceOrientationMaskLandscapeLeft|UIInterfaceOrientationMaskLandscapeRight;
}

- (void)viewWillLayoutSubviews {
    [super viewWillLayoutSubviews];

    /// 主要调整 状态栏 ios(6,9)
//    UIInterfaceOrientation orientation = [[UIApplication sharedApplication] statusBarOrientation];
//    [[UIApplication sharedApplication] setStatusBarOrientation:orientation];
//    [[UIApplication sharedApplication] setStatusBarHidden:NO];

//    CGFloat cententY = self.view.centerY_sd - 115 - 20;
//    _noneNetworkImgButton.sd_resetLayout
//    .centerYIs(cententY)
//    .widthIs(170)
//    .heightIs(115)
//    .centerXEqualToView(_noneNetworkControl);
}


- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
}

- (void)viewDidDisappear:(BOOL)animated {
    [super viewDidDisappear:animated];
    RX_STATUS_BAR_STOP();
}

- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];

    self.navPopGestureRecognizerEnable = _navPopGestureRecognizerEnable;

    if(!self.isRotate) {
        [self closeDeviceCrossScreen];
    }

    [self.view bringSubviewToFront:_noneNetworkControl];
}

- (void)closeDeviceCrossScreen {
    UIDevice *device = [UIDevice currentDevice];

    UIDeviceOrientation orientation = [UIDevice currentDevice].orientation;
    UIInterfaceOrientation interfaceOrientation = (UIInterfaceOrientation)orientation;
    if(rx_isPad) {
        if (interfaceOrientation != UIInterfaceOrientationPortrait &&  interfaceOrientation != UIInterfaceOrientationLandscapeLeft && interfaceOrientation != UIInterfaceOrientationLandscapeRight) {
            [device setValue:[NSNumber numberWithInteger:UIInterfaceOrientationPortrait] forKey:@"orientation"];
        }
    }
    else {
        if (interfaceOrientation != UIInterfaceOrientationPortrait) {
            [device setValue:[NSNumber numberWithInteger:UIInterfaceOrientationPortrait] forKey:@"orientation"];
        }
    }
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    //状态栏影响UIScrollView的内容
//    if(RX_iOSAVALIABLE(7, 11)) {
//        self.automaticallyAdjustsScrollViewInsets = NO;
//    }
//
    // 如果需要 controller.veiw 铺满 nav 和 tabbar的位置，需要设置下面
    //    self.edgesForExtendedLayout = UIRectEdgeNone;
    self.edgesForExtendedLayout = UIRectEdgeAll;

    //一定有要底色
    self.view.backgroundColor = [UIColor whiteColor];


    //允许屏幕旋转
    self.isRotate = rx_isPad;

    //允许 右滑返回手势
    self.navPopGestureRecognizerEnable = YES;

    RX_NOTI_ADD_OBNAME(@selector(reloadNetworkStatus), NOTINAME_NETWORK);

    _viewKeyboardDeal = RXCREATE_ALLOC(ViewKeyboardDeal);
    _viewKeyboardDealTap = RXCREATE_ALLOC(UITapGestureRecognizer);
    [self addTapGestureForView:self.view];

    [self networkStatusEqualNoneCreateButton];
}

- (void)setNavPopGestureRecognizerEnable:(BOOL)navPopGestureRecognizerEnable {
    if(self.peekPop3DtouchNavPopGesEnable) {
        self.navigationController.interactivePopGestureRecognizer.enabled = YES;
        return;
    }
    _navPopGestureRecognizerEnable = navPopGestureRecognizerEnable;
    if (self.navigationController.viewControllers.count > 1) {
        self.navigationController.interactivePopGestureRecognizer.enabled = _navPopGestureRecognizerEnable;
    }
    else {
        self.navigationController.interactivePopGestureRecognizer.enabled = NO;
        _navPopGestureRecognizerEnable = NO;
    }
}


- (void)reloadNetworkStatus {
//    NSString * status = [SCMNetworkCheck shareNetworkCheck].statusString;
//    [self setNetworkStatus:status];
}

- (void)setNetworkStatus:(NSString *)networkStatus {
//    if(StrBool(_networkStatus)) {
//        _networkStatus = networkStatus;
//        [self networkChange];
//    }
//    else {
//        _networkStatus = networkStatus;
//    }


}

- (void)showToash:(NSString *)message {
    UIView * navView = self.view;
    if(self.navigationController) {
        navView = self.navigationController.view;
    }
//    MBProgressHUD * hud = [MBProgressHUD showHUDAddedTo:navView animated:YES];
//    [hud showHudLabel:NonEmptyString(message)];
}

- (void)networkChange { /*子类去写*/ }

// 显示没有网络状态  默认从顶部64开始
- (void)showNullNetworkView {
    _noneNetworkControl.hidden = NO;
    _noneNetworkControl.alpha = 1;
}

/// 隐藏没有网络状态
- (void)hiddenNullNetworkView {
    _noneNetworkControl.alpha = 1;
    _noneNetworkControl.hidden = YES;
}

/// 刷新数据源---重新请求数据-注意清空分页数据源
- (void)networkReloadDataButtonClick { /*子类去写*/ }

- (void)tabBarItemDoubleClick { /*子类去写*/ }

#pragma mark - ~~~~~~~~~~~ 无网络状态页面 ~~~~~~~~~~~~~~~
- (void)networkStatusEqualNoneCreateButton {
    //----------------------------------------------------
    //初始化
    _noneNetworkControl = [UIControl new];
    [_noneNetworkControl addTarget:self action:@selector(networkReloadDataButtonClick) forControlEvents:UIControlEventTouchUpInside];
    _noneNetworkControl.hidden = YES;
    _noneNetworkControl.backgroundColor = RXColorHexStr(@"#f0eff5");
    [self.view addSubview:_noneNetworkControl];

    //170x115
    _noneNetworkImgButton = [UIButton buttonWithType:UIButtonTypeCustom];
//    [_noneNetworkImgButton setImage:[UIImage imageNamed:@"img_basevc_bad_network"] forState:UIControlStateNormal];
    [_noneNetworkImgButton addTarget:self action:@selector(networkReloadDataButtonClick) forControlEvents:UIControlEventTouchUpInside];
    [_noneNetworkControl addSubview:_noneNetworkImgButton];


    _noneNetworkLabelButton = [UIButton buttonWithType:UIButtonTypeCustom];
    _noneNetworkLabelButton.titleLabel.numberOfLines = 0;
    [_noneNetworkLabelButton setAttributedTitle:[self badNetworkText] forState:UIControlStateNormal];
    [_noneNetworkLabelButton addTarget:self action:@selector(networkReloadDataButtonClick) forControlEvents:UIControlEventTouchUpInside];
    [_noneNetworkControl addSubview:_noneNetworkLabelButton];


    //----------------------------------------------------
    //约束
//    _noneNetworkControl.sd_layout.spaceToSuperView(UIEdgeInsetsZero);

//    _noneNetworkImgButton.sd_layout
//    .topSpaceToView(_noneNetworkControl, 130)
//    .widthIs(170)
//    .heightIs(115)
//    .centerXEqualToView(_noneNetworkControl);

//    _noneNetworkLabelButton.sd_layout
//    .leftEqualToView(_noneNetworkControl)
//    .rightEqualToView(_noneNetworkControl)
//    .topSpaceToView(_noneNetworkImgButton, 15)
//    .heightIs(15);
}

- (NSAttributedString *)badNetworkText {
    NSString * bigText = @"没有网络";
    NSString * infoText = @"\n点击屏幕重新加载";
    bigText = [bigText stringByAppendingString:infoText];
    NSMutableAttributedString * attri = [[NSMutableAttributedString alloc] initWithString:bigText];
    [attri setAttributes:@{NSFontAttributeName:RX_FontBase(14), NSForegroundColorAttributeName:RXColorHexStr(@"222222")} range:NSMakeRange(0, attri.length)];
    [attri addAttributes:@{NSFontAttributeName:RX_FontBase(16)} range:NSMakeRange(0, attri.length - infoText.length)];
//#if DEBUG
//    [attri addAttributes:@{NSBackgroundColorAttributeName:[UIColor blueColor]} range:NSMakeRange(0, attri.length)];
//#endif
    NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
    [paragraphStyle setLineSpacing:4];
    [paragraphStyle setLineBreakMode:NSLineBreakByWordWrapping];
    [paragraphStyle setAlignment:NSTextAlignmentCenter];
    [attri addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, attri.length)];
    return attri;
}

- (void)pushController:(Class)controllerClass {
    if (!self.navigationController) return;
    UIViewController * c = RXCREATE_ALLOC_STR(controllerClass);
    [self.navigationController pushViewController:c animated:YES];
}
- (void)pop {
    if (!self.navigationController) return;
    [self.navigationController popViewControllerAnimated:YES];
}
- (void)popToTop {
    if (!self.navigationController) return;
    [self.navigationController popToRootViewControllerAnimated:YES];
}

- (void)dealloc {
    RX_NOTI_REMOVE_SELF;
//    RXLog(@"className=%@ -- 当前页面没有 被循环引用", NSStringFromClass([self class]));
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/


- (void)addTapGestureForView:(UIView *)view {
    if(self.viewKeyboardDeal.associatedView ) {
        [self.viewKeyboardDeal.associatedView removeGestureRecognizer:self.viewKeyboardDealTap];
    }
    self.viewKeyboardDeal.associatedView = view;
    self.viewKeyboardDealTap.delegate = self.viewKeyboardDeal;
    [view addGestureRecognizer:self.viewKeyboardDealTap];
}

@end



@implementation ViewKeyboardDeal
- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer
{
    [self.associatedView endEditing:YES];
    return NO;
}
@end
